package org.zpmis.api.http;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zpmis.api.business.CompanyWebService;
import org.zpmis.common.base.BaseController;
import org.zpmis.common.exception.BusinessException;
import org.zpmis.common.result.PlatformResult;
import org.zpmis.domain.company.qo.*;
import org.zpmis.domain.company.vo.GetCompanyDetailVo;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
* created with CodeGenerator
* description: 企业信息控制类
* @author  Yang
* date: 2021-6-2
* time: 14:46:52
*/
@RestController
@RequestMapping("/company")
@Slf4j
public class CompanyController extends BaseController{

    @Resource
    private CompanyWebService companyWebService;


    /**
     * 注册企业信息接口
     * @param registerCompanyQo
     * @return
     */
    @PostMapping("/registerCompany")
    public PlatformResult registerCompany(@Validated @RequestBody RegisterCompanyQo registerCompanyQo){
        try {
            companyWebService.registerCompany(registerCompanyQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---registerCompany--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---registerCompany--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 后台新增企业信息接口
     * @param registerCompanyQo
     * @return
     */
    @PostMapping("/addCompany")
    public PlatformResult addCompany(@Validated @RequestBody RegisterCompanyQo registerCompanyQo){
        try {
            companyWebService.addCompany(registerCompanyQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---addCompany--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---addCompany--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 代理新增企业信息接口
     * @param registerCompanyQo
     * @return
     */
    @PostMapping("/agentAddCompany")
    public PlatformResult agentAddCompany(@Validated @RequestBody RegisterCompanyQo registerCompanyQo){
        try {
            companyWebService.addCompany(registerCompanyQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---agentAddCompany--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---agentAddCompany--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }
    /**
     * 代理修改企业信息接口
     * @param registerCompanyQo
     * @return
     */
    @PostMapping("/agentUpdateCompany")
    public PlatformResult agentUpdateCompany(@Validated @RequestBody RegisterCompanyQo registerCompanyQo){
        try {
            companyWebService.agentUpdateCompany(registerCompanyQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---agentUpdateCompany--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---agentUpdateCompany--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }
    /**
     * 审核注册企业
     * @param auditCompanyQo
     * @return
     */
    @PostMapping("/auditCompany")
    public PlatformResult auditCompany(@Validated @RequestBody AuditCompanyQo auditCompanyQo){
        try {
            companyWebService.auditCompany(auditCompanyQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---auditCompany--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---auditCompany--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     * 企业列表查询
     * @param companyListQo
     * @return
     */
    @PostMapping("/companyList")
    public PlatformResult companyList(@RequestBody CompanyListQo companyListQo){
        try {
            return PlatformResult.success(companyWebService.companyList(companyListQo));
        }catch (BusinessException be) {
            log.error("[CompanyController]---agentCompanyList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---companyList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 代理商企业列表
     * @param companyListQo
     * @return
     */
    @PostMapping("/agentCompanyList")
    public PlatformResult agentCompanyList(@RequestBody CompanyListQo companyListQo){
        try {
            //设置当前企业id为要查询的企业的上级代理id 条件查询
            companyListQo.setAgentId(super.getAppletsLoginUser().getCompanyId());
            return PlatformResult.success(companyWebService.companyList(companyListQo));
        } catch (BusinessException be) {
            log.error("[CompanyController]---agentCompanyList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---agentCompanyList--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 所有通过的企业列表查询,下拉使用
     * @param
     * @return
     */
    @PostMapping("/companyAllList")
    public PlatformResult companyAllList(@RequestBody CompanyAllListQo companyAllListQo){
        try {
            return PlatformResult.success(companyWebService.companyAllList(companyAllListQo));
        }catch (BusinessException be) {
            log.error("[CompanyController]---companyAllList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---companyAllList--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 所有通过的代理商下的企业列表查询,下拉使用(包括自己)
     * @param
     * @return
     */
    @PostMapping("/agentCompanyAllList")
    public PlatformResult agentCompanyAllList(@RequestBody AgentCompanyAllListQo agentCompanyAllListQo){
        try {
            return PlatformResult.success(companyWebService.agentCompanyAllList(agentCompanyAllListQo));
        }catch (BusinessException be) {
            log.error("[CompanyController]---agentCompanyAllList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---agentCompanyAllList--->Exception:",e);
            return PlatformResult.failure();
        }
    }
    /**
     * 企业状态变更
     * @param changeStatusQo
     * @return
     */
    @PostMapping("/changeStatus")
    public PlatformResult changeStatus(@Validated @RequestBody ChangeStatusQo changeStatusQo){
        try {
            companyWebService.changeStatus(changeStatusQo);
        } catch (BusinessException be) {
            log.error("[CompanyController]---changeStatus--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---changeStatus--->Exception:",e);
            return PlatformResult.failure();
        }
        return PlatformResult.success();
    }

    /**
     *  企业详情轨迹查询
     * @param companyTrackQo
     * @return
     */
    @PostMapping("/companyTrack")
    public PlatformResult companyTrack(@Validated @RequestBody CompanyTrackQo companyTrackQo){
        try {
            return PlatformResult.success(companyWebService.companyTrack(companyTrackQo));
        }catch (BusinessException be) {
            log.error("[CompanyController]---agentCompanyList--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[IntentionController]---companyTrack--->Exception:",e);
            return PlatformResult.failure();
        }
    }

    /**
     * 获取企业详情
     * @param getCompanyDetailQo
     * @return
     */
    @PostMapping("getCompanyDetail")
    public PlatformResult getCompanyDetail(@Valid @RequestBody GetCompanyDetailQo getCompanyDetailQo){
        try {
            return PlatformResult.success(companyWebService.getCompanyDetail(getCompanyDetailQo));
        } catch (BusinessException be) {
            log.error("[CompanyController]---getCompanyDetail--->BusinessException:",be);
            return PlatformResult.failure(be.getResultCode());
        } catch (Exception e) {
            log.error("[CompanyController]---getCompanyDetail--->Exception:",e);
            return PlatformResult.failure();
        }
    }
}
